<template>
	<view>
		<u-navbar :borderBottom="false" back-text=" " :title="title"></u-navbar>
		<view class="sthconnect">
			<view class="sthconnectInfo">
				<view class="info_item">
					<span>
						<span class="xing">*</span>
						<span class="info_item_title">购买平台</span>
					</span>
					
					<div class="info_item_right">
						<input placeholder-class="inputPlaceholder" maxlength="20" :disabled="form.status!=0" v-model="form.platform" class="input" type="text" placeholder="请输入购买平台" />
					</div>
					
				</view>
				<view class="info_item">
					<span>
						<span class="xing">*</span>	
						<span class="info_item_title">平台账号</span>
					</span>
					
					<div class="info_item_right">
						<input placeholder-class="inputPlaceholder" maxlength="20" :disabled="form.status!=0"  v-model="form.account"  class="input" type="text" placeholder="请输入平台账号" />
					</div>
					
				</view>
				<view class="info_item">
					<span>
						<span class="xing">*</span>
						<span class="info_item_title">密码</span>
					</span>
					
					<div class="info_item_right">
						<input placeholder-class="inputPlaceholder" maxlength="20" :disabled="form.status!=0" v-model="form.password" class="input" type="text" placeholder="请输入平台密码" />
					</div>
					
				</view>
				<view @click="form.status!=0?'':openPop()" class="info_item">
					<span>
						<span class="xing">*</span>
						<span class="info_item_title">寄往仓库</span>
					</span>
					<div class="info_item_right">
						<span class="input">{{type_text}}</span>
						<!-- <input  disabled class="input" type="text" placeholder="请选择寄往仓库" /> -->
						<image style="width: 24rpx; height: 24rpx; margin-left: 16rpx;" src="https://www.mengkahuyu.com/index_png/back.png" mode=""></image>
					</div>
				</view>
				<view class="info_item">
					<span>
						<span class="xing">*</span>
						<span class="info_item_title">物品名称</span>
					</span>
					<div class="info_item_right">
						<input placeholder-class="inputPlaceholder" maxlength="20" :disabled="form.status!=0" v-model="form.name" class="input" type="text" placeholder="请输入物品名称" />
					</div>
				</view>
				<view class="info_item">
					<span>
						<span class="xing">*</span>
						<span class="info_item_title">物品款式</span>
					</span>
					<div class="info_item_right">
						<input placeholder-class="inputPlaceholder" maxlength="20" :disabled="form.status!=0" v-model="form.style" class="input" type="text" placeholder="请输入物品款式" />
					</div>
				</view>
				<view class="info_item">
					<span>
						<span class="xing">*</span>
						<span class="info_item_title">数量</span>
					</span>
					<div class="info_item_right">
						<input placeholder-class="inputPlaceholder" maxlength="8" :disabled="form.status!=0"  v-model="form.num" class="input" type="number" placeholder="请输入物品数量" />
					</div>
				</view>
				<view class="info_item" style="border: none;">
					<span>
						<span class="xing"></span>
						<span class="info_item_title">备注</span>
					</span>
					<div class="info_item_right">
					</div>
				</view>
				<textarea v-model="form.remark" placeholder-class="inputPlaceholder" :disabled="form.status!=0" class="textarea" maxlength="150" placeholder="请输入备注" id="" cols="30" rows="10"></textarea>
			</view>
		</view>
		<view class="imgs">
			<view class="upimg_title">
				上传物品照片
			</view>
		</view>
		<view class="" style="padding: 0rpx 0 32rpx 32rpx;">
			<upload :imag-list="imgList" :disable='form.status!=0' @imageChange="FileChange"></upload>
		</view>
		<view class="" style="width: 100%;height: 200rpx;">
			
		</view>
		<view  class="button_submit">
			<u-button v-if="order_id!=null && form.status==0" @click="onSubmit(2)" class="btns" :custom-style="{'background':'#90c0ff' ,color:'#fff'}"  :shape="'circle'" :hair-line="false" :ripple="true" ripple-bg-color="rgba(9, 82, 200, 0.15)">
				修改
			</u-button>
			<u-button v-if="order_id!=null && form.status==1" @click="wxPay(form.id)" class="btns" :custom-style="{'background':'#90c0ff' ,color:'#fff'}"  :shape="'circle'" :hair-line="false" :ripple="true" ripple-bg-color="rgba(9, 82, 200, 0.15)">
				立即支付
			</u-button>
			<u-button v-if="order_id==null" @click="onSubmit(1)" class="btns" :custom-style="{'background':'#90c0ff' ,color:'#fff'}"  :shape="'circle'" :hair-line="false" :ripple="true" ripple-bg-color="rgba(9, 82, 200, 0.15)">
				提交审核
			</u-button>
		</view>
		<u-action-sheet :list="list" @click="onchange" v-model="pop_show"></u-action-sheet>
	</view>
</template>

<script>
	import upload from '@/componts/upload/upload.vue';
	export default {
		components:{
		    upload
		},
		data() {
			return {
				title:'申请代付',
				pop_show:false,
				type_text:'请选择寄往仓库',
				list:[
					{text:'韩国仓库' , type:1},
					{text:'日本仓库' , type:2},
				],
				imgList:[],
				order_id:null,
				form:{
					status:0,
					"type2": "2", //1 代买 2代付
					"link": null,
					"type": null, //1韩国 2日本
					"name": null, //物品名
					"platform": null, //平台
					"style": null, //款式
					"num": null, //数量
					"remark": null, //备注信息
					"pic": null, //图片
					"password": null, //代付平台密码
					"account": null //代付平台账号
				}
			}
		},
		onLoad(e) {
			if(e.order_id){
				this.order_id = e.order_id
				this.getInfo()
			}
		},
		onShow() {
			var token = uni.getStorageSync('token')
			if(!token){
				uni.setStorageSync('isModule' , true)
				uni.showModal({
					title: '登录',
					content: '登录后可使用完整功能',
					cancelText: "取消", // 取消按钮的文字  
					confirmText: "确认", // 确认按钮的文字  
					showCancel: true, // 是否显示取消按钮，默认为 true
					success: (res) => {
						uni.setStorageSync('isModule' , false)
					if(res.confirm) {  
						// console.log('comfirm') //点击确定之后执行的代码
						uni.navigateTo({
							url:'/pages/login/login'
						})
					} else {  
						uni.switchTab({
							url:'/pages/index/index'
						})
						}  
					} 
				})
				return
			}
		},
		methods: {
			wxPay(id){
				var data = {
					id:id,
				}
				uni.showLoading({
					title:'支付中',
				})
				this.$wqzApi.pay_buy_pay(data).then(res=>{
					// var data =JSON.parse(res.data)
					// 吊起微信支付
					window.location.href = res.data
					// wx.requestPayment({
					//   timeStamp: data.timeStamp,
					//   nonceStr: data.nonceStr,
					//   package: data.package,
					//   signType: 'MD5',
					//   paySign: data.paySign,
					//   success (res) {
					// 	  uni.hideLoading()
					// 	  uni.showToast({
					// 	  	title:'支付成功',
					// 		icon:'none'
					// 	  })
					// 	 this.$refs.paging.reload()	
					//   },
					//   fail (res) {
					// 	  uni.hideLoading()
					// 	  // console.log(res , '支付失败返回')
					//   }
					// })
				}).catch(err=>{
					
				})
			},	
			dignyu(){
				return new Promise((resolve, reject) => {
					wx.requestSubscribeMessage({
					    tmplIds: [
							'bjb55ZIOo1z7CjLTXkkqVSd_fe6WJLaqIjxtmihuZTI', //物流消息提醒
							'LL09Z07c3pLTnLQotLfD8DYQm9SB6A-QQ1QW3eBop2M'//支付消息提醒
						],
					    success(res) {
							resolve(true)
					    },
						fail(err) {
							resolve(false)
						}
					})
				})
			},
			openPop(){
				this.pop_show = true
			},
			getInfo(){
				// 
				var data = {
					id:this.order_id
				}
				this.$wqzApi.buy_detail(data).then(res=>{
					this.form = res.data
					if( res.data.pic!=''){
						this.imgList = res.data.pic.split(',')
					}
					this.type_text = res.data.type==1?'韩国仓库':'日本仓库'
				})
			},
			onchange(e){
				// console.log( e , '选择的仓库')
				this.type_text = this.list[e].text
				this.form.type = this.list[e].type
			},
			FileChange(e){
				// console.log(e)
				this.imgList = e
			},
			onSubmit(type){
				var isOk = true
				var toast = ''
				try{
					/**
					 * @link  链接
					 * @type  1韩国 2日本
					 * @name  物品名
					 * @platform  平台
					 * @style  款式
					 * @num   数量
					 * @remark  备注信息
					 * @pic  图片
					 * @password 代付平台密码
					 * @account 代付平台账号
					*/ 
					for(var key in this.form){
						if(!this.form[key]){
							if(key=='platform'){
								toast="请输入购买平台"
								throw new Error(toast)
							} 
							if(key=='account'){
								toast="请输入平台账号"
								throw new Error(toast)
							} 
							if(key=='password'){
								toast="请输入平台密码"
								throw new Error(toast)
							}
							if(key=='password'){
								toast="请输入平台密码"
								throw new Error(toast)
							}
							if(key=='type'){
								toast="请选择寄往仓库"
								throw new Error(toast)
							}
							if(key=='name'){
								toast="请输入物品名称"
								throw new Error(toast)
							}
							if(key=='style'){
								toast="请输入物品名款式"
								throw new Error(toast)
							}
							if(key=='num'){
								toast="请输入物品数量"
								throw new Error(toast)
							}
						}
					}
				}catch(err){
					isOk = false
				}	
				if(!isOk){
					uni.showToast({
						title:toast,
						icon:'none'
					})
					return
				}
				this.form.pic = this.imgList.join(',')
				this.dignyu().then(res=>{
					if(type==1){
						// 新增
						this.$wqzApi.Buy_add(this.form).then(res=>{
							uni.showToast({
								title:'提交成功',
								icon:'none'
							})
						})
						uni.redirectTo({
							url:'/pages/order_other_list/order_other_list?current=0&type=2'
						})
					}else{
						// 修改
						this.$wqzApi.buy_update(this.form).then(res=>{
							uni.showToast({
								title:'提交成功',
								icon:'none'
							})
						})
						uni.redirectTo({
							url:'/pages/order_other_list/order_other_list?current=0&type=2'
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import url(./index.scss)
</style>
